﻿@model ProductItem
<div class="modal-dialog">
    <div class="modal-content">
        @using (Html.BeginForm())
        {
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;</button>
                <h4 class="modal-title">
                    修改价格</h4>
            </div>
            <div class="modal-body">
                <h4>@(Model.Name)</h4>
                <div class="form-group">
                    <label for="avgprice">
                        商品价格</label>
                    <input id="avgprice" name="avgprice" type="text" maxlength="8" value='@(Model.Price.ToString("N"))' class="form-control input-xlarge" onkeyup="checkValue(this);"/>
                </div>
                @foreach (var o in Model.ProductItemSkus)
                {
                    <div class="form-group">
                        <label for="price-@(o.Id)">
                            @foreach (var s in o.SkuProps)
                            {
                                <span>@(s.VName)</span>
                            }
                        </label>
                        <input id="price-@(o.Id)" name="price" type="text" maxlength="8" value='@(o.Price.ToString("N"))' class="form-control input-xlarge" onkeyup="checkValue(this);"/>
                        <input name="id" type="hidden" value='@(o.Id)' />
                    </div>
                }
            </div>
            <div class="modal-footer">
                <p id="message" class="pull-left color-red">
                </p>
                <button type="button" class="btn btn-primary" onclick="submitForm(this.form,@(Model.Id),@(Model.ProductItemSkus.Count))">
                    确 认</button>
            </div>
        }
    </div>
</div>
<script type="text/javascript">
    function checkPrice() {
        var array = [];
        var prices = $('input[name=price]');
        var avgprice = parseInt($('#avgprice').val());
        $.each(prices, function (i, o) {
            var price = parseInt($(o).val());
            array.push(price);
        });
        var min = _.min(array);
        var max = _.max(array);
        return (min <= avgprice && avgprice <= max);
    }

    function checkValue(obj) {
        var val = $(obj).val();
        if (!isNaN(val)) {
        } else {
            $(obj).val('');
        }
    }
    function submitForm(form, itemId, skuCount) {

        if (skuCount > 0 && !checkPrice()) {
            $('#message', form).css('color', 'red').html('商品价格请设置在合理区间内！');
            return;
        }

        var param = $(form).serialize();
        $.postJSON(form.action, param, function (result) {
            if (result.Status) {
                $("#item-price-" + itemId).html(result.Data);
                $('#message', form).css('color', 'green').html("更新价格成功！");
            } else {
                $('#message', form).css('color', 'red').html(result.Message);
            }
        });
    }
</script>
